<?php
	//Symptom class
	class Symptom {
		private $symptom_id;
		private $user_id;
		private $symptom_name;
		private $symptom_level;
		private $symptom_note;
		private $symptom_time;

		//Note: Constructor does not set ID however insert() and Symptom::createSymptom() sets ID
		//      Use Symptom::createSymptom() for manipulation of Symptom
		
		//Constructor for Symptom
		//
		//Param: user id, name, level, note, time
		function __construct($user_id, $symptom_name, $symptom_level, $symptom_note, $symptom_time = null) {
			$this->setUserID($user_id);
			$this->setName($symptom_name);
			$this->setLevel($symptom_level);
			$this->setNote($symptom_note);
			$this->setTime($symptom_time);
		}
		
		//Setter for Symptom ID
		function setSymptomID($val) {
			$this->symptom_id = $val;
		}
		//Getter for Symptom ID
		function getSymptomID() {
			return $this->symptom_id;
		}
		
		//Setter for User ID
		function setUserID($val) {
			$this->user_id = $val;
		}
		//Getter for User ID
		function getUserID() {
			return $this->user_id;
		}
		
		//Setter for Symptom name
		function setName($val) {
			$this->symptom_name = $val;
		}
		//Getter for Symptom name
		function getName() {
			return $this->symptom_name;
		}
		
		//Setter for Symptom level
		function setLevel($val) {
			$this->symptom_level = $val;
		}
		//Getter for Symptom level
		function getLevel() {
			return $this->symptom_level;
		}
		
		//Setter for Symptom note
		function setNote($val) {
			$this->symptom_note = $val;
		}
		//Getter for Symptom note
		function getNote() {
			return $this->symptom_note;
		}
		
		//Setter for Symptom time
		function setTime($val = null) {
			if ($val === null) {
				$this->symptom_time = date(DATE_FORMAT);
			} else {
				$this->symptom_time = $val;
			}
		}					
		//Getter for Symptom time
		function getTime() {
			return $this->symptom_time;
		}
		
		//Insert new Symptom info into database
		//Sets Symptom ID to current Symptom
		//
		//Return: Symptom ID or O
		function insert() {
			$db = Database::get();
			$query = "INSERT INTO " . SYMPTOM_TABLE . " 
				(user_id, symptom_name, symptom_level, symptom_note, created_at)
				VALUES(?, ?, ?, ?, ?);";
			$stmt = $db->prepare($query);
			if ($stmt) {

				$user_id = $this->getUserID();
				$symptom_name = $this->getName();
				$symptom_level = $this->getLevel();
				$symptom_note = $this->getNote();
				$symptom_time = $this->getTime();
				
				$stmt->bind_param("dsdss", $user_id, $symptom_name, $symptom_level, $symptom_note, $symptom_time);
				$stmt->execute() or die('Invalid query for Symptom: ' . $db->error);
				$symptom_id = $stmt->insert_id;
				$this->setSymptomID($symptom_id);
				$stmt->close();
				
				//Create notification for the doctor to input into database
				//Can use str_replace for arrays but too troublesome, will edit in future
				$notification_message = str_replace('PERSON', User::findName($user_id), SYMPTOM_DOCTOR_MSG);
				$notification_message = str_replace('SYMPTOM', $symptom_name, $notification_message);
				$notification_message = str_replace('LEVEL', $symptom_level, $notification_message);
				$notification_type_id = Notification::findTypeID('symptom_doctor');

				$doctor_list = Patient::doctorList($user_id);
				if ($doctor_list !== null) {
					foreach ($doctor_list as $doctor) {			
						$doctor_id = $doctor['doctor_id'];
						if (Patient::checkDoctor($doctor_id, $user_id)) {
							$notification = new Notification($doctor_id, $notification_type_id, $notification_message, $user_id, $symptom_id, $symptom_time);
							$notification->insert();
						}
					}
				}
				
				return intval($symptom_id);
				
			}
			return 0;
		}
		
		//Remove Symptom from database
		//
		//Return: true or false
		function remove() {
			$db = Database::get();
			$query = "DELETE FROM " . SYMPTOM_TABLE . " 
				WHERE symptom_id = ?;";
			$stmt = $db->prepare($query);
			if ($stmt) {
				$symptom_id = $this->getSymptomID();
				$stmt->bind_param("d", $symptom_id);
				$stmt->execute() or die('Invalid query for Symptom: ' . $db->error);
				$stmt->close();
				
				//Create notification for the doctor to delete from database
				$user_id = $this->getUserID();
				$doctor_list = Patient::doctorList($user_id);
				
				$notification_type_id = Notification::findTypeID('symptom_doctor');
				$notification_time = $this->getTime();
				
				if ($doctor_list !== null) {
					foreach ($doctor_list as $doctor) {			
						$doctor_id = $doctor['doctor_id'];
						if (Patient::checkDoctor($doctor_id, $user_id)) {
							$notification_id = Notification::findIDWithLog($doctor_id, $notification_type_id, $user_id, $symptom_id);
							$notification = Notification::createNotification($notification_id);
							$notification->remove();
						}
					}
				}						
				return true;
			}
			return false;
		}
		
		//Create Symptom object given the ID by taking information from the database
		//Information taken from the database: user id, name, level, note, time
		//
		//Param: Symptom id
		//Return: Symptom object
		static function createSymptom($id) {
			$db = Database::get();
			$query = "SELECT * FROM " . SYMPTOM_TABLE . "
					  WHERE symptom_id = ?;";
			$stmt = $db->prepare($query);
			if ($stmt) {
				$stmt->bind_param("d", $id);
				$stmt->execute() or die('Invalid query for Symptom: ' . $db->error);
				$result = $stmt->get_result();
				$symptom_row = null;
				if ($row = $result->fetch_assoc()) {
					$symptom_row = $row;
				} 
				$stmt->close();
			} else {
				die('Invalid query for Symptom: ' . $db->error);
			}
			
			$symptom = null;
			if ($symptom_row !== null) {
				$user_id = $symptom_row['user_id'];
				$symptom_name = $symptom_row['symptom_name'];
				$symptom_level = $symptom_row['symptom_level'];
				$symptom_note = $symptom_row['symptom_note'];
				$symptom_time = $symptom_row['created_at'];
				
				$symptom = new Symptom($user_id, $symptom_name, $symptom_level, $symptom_note, $symptom_time);
				$symptom->setSymptomID($id);
				}
				
				
			return $symptom;
		}
		
		//Get list of Symptom given the user id
		//
		//Param: user id
		//Return: List of Symptom
		static function getSymptomList($user_id) {
			$db = Database::get();
			$query = "SELECT * FROM " . SYMPTOM_TABLE . "
					  WHERE user_id = ?
					  ORDER BY created_at;";
			$stmt = $db->prepare($query);
			if ($stmt) {
				$stmt->bind_param("d", $user_id);
				$stmt->execute() or die('Invalid query for Symptom: ' . $db->error);
				$result = $stmt->get_result();
				$symptom_list = null;
				while ($row = $result->fetch_assoc()) {
					$symptom_list[] = $row;
				} 
				$stmt->close();
			} else {
				die('Invalid query for Symptom: ' . $db->error);
			}
			
			return $symptom_list;
		}			
	}
?>